<?php
/*首页数据*/
namespace Admin\Model;
use Think\Model;
class StockModel extends Model {
    public function addModelStock($data){
        $carprice = M('car_price');
        $isnewarr = array(
            'is_new'=>0,
        );
        $resversion  = $carprice->where(array('is_new'=>1,'carnum'=>$data['carnum']))->find();
        $arraydata[] = array(
            'pay_month'=>$data['pay_month0'],
            'surplus_money'=>$data['surplus_money0'],
            'surplus_month_pay'=>$data['surplus_month_pay0'],
            'surplus_pay_month'=>$data['surplus_pay_month0'],
            'guide_price'=>$data['guide_price'],
            'jdc_price'=>$data['jdc_price'],
            'preferential_ratio'=>$data['preferential_ratio'],
            'first_pay'=>$data['first_pay'],
            'month_pay'=>$data['month_pay'],
            'is_new'=>1,
            'version'=>$resversion['version']+1,
            'carnum'=>$data['carnum'],

            );
        $arraydata[] = array(
            'pay_month'=>$data['pay_month1'],
            'surplus_money'=>$data['surplus_money1'],
            'surplus_month_pay'=>$data['surplus_month_pay1'],
            'surplus_pay_month'=>$data['surplus_pay_month1'],

            'guide_price'=>$data['guide_price'],
            'jdc_price'=>$data['jdc_price'],
            'preferential_ratio'=>$data['preferential_ratio'],
            'first_pay'=>$data['first_pay'],
            'month_pay'=>$data['month_pay'],
            'is_new'=>1,
            'version'=>$resversion['version']+1,
            'carnum'=>$data['carnum'],

            );
        $arraydata[] = array(
            'pay_month'=>$data['pay_month2'],
            'surplus_money'=>$data['surplus_money2'],
            'surplus_month_pay'=>$data['surplus_month_pay2'],
            'surplus_pay_month'=>$data['surplus_pay_month2'],

            'guide_price'=>$data['guide_price'],
            'jdc_price'=>$data['jdc_price'],
            'preferential_ratio'=>$data['preferential_ratio'],
            'first_pay'=>$data['first_pay'],
            'month_pay'=>$data['month_pay'],
            'is_new'=>1,
            'version'=>$resversion['version']+1,
            'carnum'=>$data['carnum'],

            );
        $arraydata[] = array(
            'pay_month'=>$data['pay_month3'],
            'surplus_money'=>$data['surplus_money3'],
            'surplus_month_pay'=>$data['surplus_month_pay3'],
            'surplus_pay_month'=>$data['surplus_pay_month3'],

            'guide_price'=>$data['guide_price'],
            'jdc_price'=>$data['jdc_price'],
            'preferential_ratio'=>$data['preferential_ratio'],
            'first_pay'=>$data['first_pay'],
            'month_pay'=>$data['month_pay'],
            'is_new'=>1,
            'version'=>$resversion['version']+1,
            'carnum'=>$data['carnum'],

            );
        $arraydata[] = array(
            'pay_month'=>$data['pay_month4'],
            'surplus_money'=>$data['surplus_money4'],
            'surplus_month_pay'=>$data['surplus_month_pay4'],
            'surplus_pay_month'=>$data['surplus_pay_month4'],

            'guide_price'=>$data['guide_price'],
            'jdc_price'=>$data['jdc_price'],
            'preferential_ratio'=>$data['preferential_ratio'],
            'first_pay'=>$data['first_pay'],
            'month_pay'=>$data['month_pay'],
            'is_new'=>1,
            'version'=>$resversion['version']+1,
            'carnum'=>$data['carnum'],

            );
        $arraydata[] = array(
            'pay_month'=>$data['pay_month5'],
            'surplus_money'=>$data['surplus_money5'],
            'surplus_month_pay'=>$data['surplus_month_pay5'],
            'surplus_pay_month'=>$data['surplus_pay_month5'],

            'guide_price'=>$data['guide_price'],
            'jdc_price'=>$data['jdc_price'],
            'preferential_ratio'=>$data['preferential_ratio'],
            'first_pay'=>$data['first_pay'],
            'month_pay'=>$data['month_pay'],
            'is_new'=>1,
            'version'=>$resversion['version']+1,
            'carnum'=>$data['carnum'],

            );
        $arraydata[] = array(
            'pay_month'=>$data['pay_month6'],
            'surplus_money'=>$data['surplus_money6'],
            'surplus_month_pay'=>$data['surplus_month_pay6'],
            'surplus_pay_month'=>$data['surplus_pay_month6'],

            'guide_price'=>$data['guide_price'],
            'jdc_price'=>$data['jdc_price'],
            'preferential_ratio'=>$data['preferential_ratio'],
            'first_pay'=>$data['first_pay'],
            'month_pay'=>$data['month_pay'],
            'is_new'=>1,
            'version'=>$resversion['version']+1,
            'carnum'=>$data['carnum'],

            );
        $arraydata[] = array(
            'pay_month'=>$data['pay_month7'],
            'surplus_money'=>$data['surplus_money7'],
            'surplus_month_pay'=>$data['surplus_month_pay7'],
            'surplus_pay_month'=>$data['surplus_pay_month7'],
            'guide_price'=>$data['guide_price'],
            'jdc_price'=>$data['jdc_price'],
            'preferential_ratio'=>$data['preferential_ratio'],
            'first_pay'=>$data['first_pay'],
            'month_pay'=>$data['month_pay'],
            'is_new'=>1,
            'version'=>$resversion['version']+1,
            'carnum'=>$data['carnum'],
            );
        $arraydata[] = array(
            'pay_month'=>$data['pay_month8'],
            'surplus_money'=>$data['surplus_money8'],
            'surplus_month_pay'=>$data['surplus_month_pay8'],
            'surplus_pay_month'=>$data['surplus_pay_month8'],

            'guide_price'=>$data['guide_price'],
            'jdc_price'=>$data['jdc_price'],
            'preferential_ratio'=>$data['preferential_ratio'],
            'first_pay'=>$data['first_pay'],
            'month_pay'=>$data['month_pay'],
            'is_new'=>1,
            'version'=>$resversion['version']+1,
            'carnum'=>$data['carnum'],
            );
        $arraydata[] = array(
            'pay_month'=>$data['pay_month9'],
            'surplus_money'=>$data['surplus_money9'],
            'surplus_month_pay'=>$data['surplus_month_pay9'],
            'surplus_pay_month'=>$data['surplus_pay_month9'],
            'guide_price'=>$data['guide_price'],
            'jdc_price'=>$data['jdc_price'],
            'preferential_ratio'=>$data['preferential_ratio'],
            'first_pay'=>$data['first_pay'],
            'month_pay'=>$data['month_pay'],
            'is_new'=>1,
            'version'=>$resversion['version']+1,
            'carnum'=>$data['carnum'],
            );
        $arraydata[] = array(
            'pay_month'=>$data['pay_month10'],
            'surplus_money'=>$data['surplus_money10'],
            'surplus_month_pay'=>$data['surplus_month_pay10'],
            'surplus_pay_month'=>$data['surplus_pay_month10'],
            'guide_price'=>$data['guide_price'],
            'jdc_price'=>$data['jdc_price'],
            'preferential_ratio'=>$data['preferential_ratio'],
            'first_pay'=>$data['first_pay'],
            'month_pay'=>$data['month_pay'],
            'is_new'=>1,
            'version'=>$resversion['version']+1,
            'carnum'=>$data['carnum'],
            );
        $arraydata[] = array(
            'pay_month'=>$data['pay_month11'],
            'surplus_money'=>$data['surplus_money11'],
            'surplus_month_pay'=>$data['surplus_month_pay11'],
            'surplus_pay_month'=>$data['surplus_pay_month11'],
            'guide_price'=>$data['guide_price'],
            'jdc_price'=>$data['jdc_price'],
            'preferential_ratio'=>$data['preferential_ratio'],
            'first_pay'=>$data['first_pay'],
            'month_pay'=>$data['month_pay'],
            'is_new'=>1,
            'version'=>$resversion['version']+1,
            'carnum'=>$data['carnum'],
            );
        $datacars    = array(
                'first_pay'=>$data['first_pay'],
                'month_pay'=>$data['month_pay'],
                'guide_price'=>$data['guide_price'],
                'jdc_price'=>$data['jdc_price'],
                'preferential_ratio'=>$data['preferential_ratio'],
                );
        foreach ($arraydata as $key => $value) {
            if(!$value['surplus_money']){
                return ["ret"=>"ERROR",'msg'=>"还有尾款未填写"];
            }
            if(!$value['surplus_month_pay']){
                 return ["ret"=>"ERROR",'msg'=>"还有尾款月供未填写"];
            }
            // if(!$value['pay_month_name']){
            //     return ["ret"=>"ERROR",'msg'=>"还有尾款期数未填写"];
            // }
            if(!$value['surplus_pay_month']){
                return ["ret"=>"ERROR",'msg'=>"还有尾款期数未填写"];
            }
        }
        $jdcCarsModel = M('cars');
        $result  = $carprice->where('carnum = '.$data['carnum'])->data($isnewarr)->save();
        $resCars = $jdcCarsModel->where('carnum = '.$data['carnum'])->data($datacars)->save();
        if($result && $resCars){
            $res = $carprice->addAll($arraydata);
            if($res){
                return true;
            }else{
                return false;
            }
        }else{
          return false;
        }  
    } 
}


?>